<!-- 计划 -->
<template>
	<el-drawer v-model="isShowDrawer" direction="rtl" :before-close="handleClose" :size="curDrawerSize" :close-on-click-modal="isReadOnly">
		<template #header="{ titleId, titleClass }">
			<div class="zs-drawer-header" :id="titleId" :class="titleClass">
				<i class="icon-b-item zs-icon zs-text-blue zs-space-right-sm"></i>
				<div class="zs-title zs-text-black">证件</div>
			</div>
		</template>

		<template #default>
			<el-container class="zs-drawer-container">
				<el-scrollbar class="zs-drawer-content">
					<!-- card1 -->
					<div class="zs-align-center zs-space-bottom" id="sm-drawer-demo-card-01">
						<i class="icon-s-item zs-icon zs-text-blue zs-space-right-sm"></i>
						<div class="zs-title">证件信息</div>
					</div>
					<div class="zs-card zs-space-bottom">
						<HsForm :filed-list="licinseInfoFiledList" ref="infoForm" @selectChange="selectChange" :isDisabled="isReadOnly"></HsForm>
					</div>
					<!-- card3 -->
					<EffectAndFailureTime ref="effectTimeRef" title="生效状态" id="effect-and-failure-time" :isReadOnly="isReadOnly" labelWidth="110px" />
					<!-- card4 -->
					<CreateUserInfo v-if="formData.data.id" ref="createDataRef" title="创建信息" id="create-user-info" />
				</el-scrollbar>
				<hsDocWrapper :nodeList="curNodeList" />
			</el-container>
		</template>

		<template v-if="handleType != 'info'" #footer>
			<div>
				<el-button class="zs-btn-md zs-btn-primary" @click="doConfirm">提交</el-button>
			</div>
		</template>
	</el-drawer>
</template>

<script setup>
	import EffectAndFailureTime from '@/components/effectAndFailureTime.vue';
	import CreateUserInfo from '@/components/createUserInfo.vue';
	import HsForm from '@/components/hsForm.vue';
	import { licinseInfoFiledList } from '@/config/form/licinse.js';
	import { licenseAddApi, licenseUpdateApi, licenseInfoApi } from '@/api/license.js';
	import { ElMessage } from 'element-plus';
	import { ref, reactive, watch, nextTick } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';
	import { deepClone } from '@/utils/publicUtils';
	import { setFormSelectOptions } from '@/utils/formEvent';

	const route = useRoute();

	const props = defineProps({
		isShow: { type: Boolean, default: false },
		isReadOnly: { type: Boolean, default: true },
		handleType: { type: String, default: '' },
		currentRow: { type: Object },
	});

	let store = useStore();
	let dictObj = store.getters.getDictObj(['PROPERTY_CERTIFICATE_TYPE']);
	const curDrawerSize = ref(store.state.uiDrawerSize);

	const emit = defineEmits(['close']);

	let isShowDrawer = ref(props.isShow);

	let formData = reactive({ data: { attachmentUrlList: [] } });

	const curNodeList = ref([
		{ name: '证件信息', id: 'sm-drawer-demo-card-01' },
		{ name: '生效状态', id: 'effect-and-failure-time' },
	]);

	const infoForm = ref();
	const forms = [infoForm];
	let effectTimeRef = ref();
	let createDataRef = ref();

	watch(
		() => props.isShow,
		(newVal, oldVal) => {
			isShowDrawer.value = newVal;
			nextTick(() => {
				newVal && props.handleType == 'add' && initForm();
				newVal && ['info', 'edit'].includes(props.handleType) && getInfo(props.currentRow?.id);
			});
		},
	);

	//详情
	const getInfo = async id => {
		let res = await licenseInfoApi({ id: id });
		if (res.data.code !== 200) return;
		let existCreate = curNodeList.value.find(item => item.id === 'create-user-info');
		!existCreate && curNodeList.value.push({ name: '创建信息', id: 'create-user-info' });
		res.data.data.attachmentUrlList = res.data.data.attachmentUrlList ? res.data.data.attachmentUrlList : [];
		res.data.data.attachmentUrlList.forEach(item => {
			item.url = item.absolutePath;
			item.name = item.attachmentName;
		});
		formData.data = Object.assign(res.data.data);
		infoForm.value.setFiledVal(formData.data);
		effectTimeRef.value.setTime({ effectTime: formData.data.startTime, expireTime: formData.data.endTime });
		nextTick(() => {
			createDataRef.value.setData(formData.data);
		});
	};

	//提交
	const doConfirm = async () => {
		let modleInfo = await infoForm.value.validateForm();
		let modleTime = await effectTimeRef.value.validateForm();
		if (!modleInfo || !modleTime) return;
		let params = Object.assign(modleInfo, modleTime);
		params.projectCode = route.query.projectCode;
		params.attachmentUrlList.forEach(item => {
			item.attachmentName = item.attachmentName || item.originalName;
			item.attachmentUrl = item.attachmentUr || item.relativePath;
		});
		let res = formData.data.id ? await licenseUpdateApi(params) : await licenseAddApi(params);
		if (res.data.code == 200) {
			ElMessage({
				message: '提交成功',
				type: 'success',
			});
			isShowDrawer.value = false;
			emit('close', { isShow: false, isRefresh: true });
		}
	};

	//关闭抽屉
	const handleClose = done => {
		effectTimeRef.value.restTime();
		emit('close', { isShow: false });
		done();
	};

	//表单初始化
	const initForm = () => {
		formData.data = {};
		infoForm.value.resetFields();
		infoForm.value.setFiledVal({ attachmentUrlList: [] });
		effectTimeRef.value.setTime({});
		let existCreate = curNodeList.value.find(item => item.id === 'create-user-info');
		existCreate && curNodeList.value.pop();
	};

	//表单关联
	const selectChange = data => {};

	setFormSelectOptions(licinseInfoFiledList, 'licenseType', dictObj.PROPERTY_CERTIFICATE_TYPE);
</script>
